﻿<#*********************************************************************

 This script is used to rename the search service administration database.
 You can rename the others using the central admin UI, go to manage
 search application, topology.  You can rename all but the administration
 database.
 
 INSTRUCTIONS
 IMPORTANT Copy the existing search database to the new name first
 
 To clean up the old database,
 Run the following query on the config database (Substitute config db and old search db name).
 
 SELECT [Id] FROM [SPS2010_TASPO_Farm_Config].[dbo].[Objects]
  where Name = 'Search_Service_Application_DB_0a464d4931c7471b8ec5037274284f09'
  
 The run this command passing the ID you get from the query, example
 stsadm -o deleteconfigurationobject -id 5E74B69F-1150-46CB-9DB1-7AEFB72EDF8C
**********************************************************************#>
param($serviceApplicationName, $newDatabaseName)

if ((Get-PSSnapin -Name  Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Add-PsSnapin  Microsoft.SharePoint.PowerShell 
}

$sa = Get-SPEnterpriseSearchServiceApplication $serviceApplicationName
if($sa -eq $null)
{
	throw "Could not find $serviceApplicationName"
}

Write-Host "Pausing $serviceApplicationName"
$sa.Pause()
$sa | Set-SPEnterpriseSearchServiceApplication -DatabaseName $newDatabaseName
Write-Host "Wating for search instance to come online"
Do {write-host -NoNewline .;Sleep 10; $searchInstance = Get-SPEnterpriseSearchServiceInstance -Local} while ($searchInstance.Status -ne "Online")